package com.google.android.apps.mytracks.io.fusiontables;

//import com.google.android.apps.mytracks.io.docs.SendDocsActivity;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.io.sendtogoogle.AbstractSendActivity;
import com.google.android.apps.mytracks.io.sendtogoogle.AbstractSendAsyncTask;
import com.google.android.apps.mytracks.io.sendtogoogle.SendRequest;
//import com.google.android.apps.mytracks.io.sendtogoogle.UploadResultActivity;
//import com.google.android.maps.mytracks.R;
//import com.google.common.annotations.VisibleForTesting;


import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.Intent;
import android.gpsmessenger.R;
import android.gpsmessenger.Trip;
import android.location.Location;
import android.location.LocationManager;

/**
 * Heavily modified code for the AndroidGpsMessenger application
 * borrowed from the open source MyTracks Application when we thought we were going to use the MyTracksAPI
 *
 * @author David Ellis
 */
public class SendFusionTablesActivity extends AbstractSendActivity {

	/* HACK - need to provide ASyncTask with selected Trip...  */
	public static Trip MyTrip;
	
  @Override
  protected AbstractSendAsyncTask createAsyncTask() {
	  
	  AccountManager mgr = AccountManager.get(getApplicationContext());
//	  Account[] accounts = AccountManager.get(this).getAccountsByType(Constants.ACCOUNT_TYPE);
	  Account[] accounts = mgr.getAccountsByType(Constants.ACCOUNT_TYPE);

	    if (accounts.length == 0) {
//	      showDialog(DIALOG_NO_ACCOUNT_ID);
//	      return;
	    }
	  
	    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    return new SendFusionTablesAsyncTask(this, sendRequest.getTrackId(), accounts[0], location, MyTrip);//sendRequest.getAccount());
  }

  @Override
  protected String getServiceName() {
    return getString(R.string.send_google_fusion_tables);
  }

  @Override
  protected void startNextActivity(boolean success, boolean isCancel) {
    sendRequest.setFusionTablesSuccess(success);
//    Class<?> next = getNextClass(sendRequest, isCancel);
//    Intent intent = IntentUtils.newIntent(this, next)
//        .putExtra(SendRequest.SEND_REQUEST_KEY, sendRequest);
//    startActivity(intent);
    finish();
  }
  
//  @VisibleForTesting
//  Class<?> getNextClass(SendRequest request, boolean isCancel) {
//  if (isCancel) {
//        return UploadResultActivity.class;
//      } else {
//        if (request.isSendDocs()) {
//          return SendDocsActivity.class;
//        } else {
//          return UploadResultActivity.class;
//        }
//      }
//  }
}
